using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using umbraco.cms.businesslogic.media;
using Commerce.Common;
using Commerce.Promotions;
using System.Text.RegularExpressions;

namespace CommerceForUmbraco.usercontrols
{
    public partial class ProductCrossSell : System.Web.UI.UserControl
    {
        protected DataTable dtCross;

        protected int _relatedProductID = 0;

        public string relatedProductID
        {
            get { return _relatedProductID.ToString(); }
            set 
            {
                if (Int32.TryParse(value, out _relatedProductID))
                {
                }
                else
                {
                    _relatedProductID = 0;
                }
            }
        }

        public int DefaultImage
        {
            get;
            set;
        }

        public void LoadCrossSells()
        {
            if (_relatedProductID != 0)
            {
                //Commerce.Common.ProductCrossSellCollection crossSell = new ProductCrossSellCollection();
                //crossSell.Where("productID", _relatedProductID);
                //crossSell.Load();

                //DataTable dt = crossSell.ToDataTable();

                //if (dt != null)
                //{
                //    if (dt.Rows.Count > 0)
                //    {
                //        foreach (DataRow dr in dt.Rows)
                //        {
                //            int mediaId = int.Parse(dr["defaultImage"].ToString());
                //            if (Int32.TryParse(dr["defaultImage"].ToString(), out mediaId))
                //            {
                //                dr["defaultImage"] = new Media(mediaId).getProperty("umbracoFile").Value.ToString();
                //            }
                //        }

                //        CrossSellProducts.DataSource = dt;
                //        CrossSellProducts.DataBind();
                //    }
                //    else
                //    {
                //        CrossSellProducts.Visible = false;
                //    }
                //}

                Commerce.Common.ProductCollection crosses = PromotionService.GetCrossSells(_relatedProductID).OrderByAsc("productName");

                DataTable dt = new DataTable();
                dt.Columns.Add("productId");
                dt.Columns.Add("urlFriendly");
                dt.Columns.Add("productName");
                dt.Columns.Add("defaultImage");
                dt.Columns.Add("productPrice");

                foreach (Product cross in crosses)
                {
                    int mediaId;
                    string newDefaultImage = "";

                    if (Int32.TryParse(cross.DefaultImage, out mediaId))
                    {
                        if (mediaId > 0)
                        {
                            if (umbraco.cms.businesslogic.media.Media.IsNode(mediaId))
                            {
		                        Media temp = new Media(mediaId);
                                if (temp != null)
                                {
			                        if (temp.getProperty("crops").Value.ToString().Trim().Length > 0)
			                        {
			                            string imagecrops = temp.getProperty("crops").Value.ToString();

			                            // Regex to pull out crop out of xml
			                            string pattern = @"<crop name=""Large Thumbnail"".+url=""(?'url'.*_Large Thumbnail\.jpg?)"" />";
			                            MatchCollection matches = Regex.Matches(imagecrops, pattern);

			                            string imageloc = "";

			                            foreach (Match match in matches)
			                            {
			                                if (match.Groups["url"].Success == true)
			                                {
			                                    imageloc = match.Groups["url"].Value;
			                                }
			                            }

			                            newDefaultImage = Server.UrlEncode(imageloc);
			                        }
			                        else if (temp.getProperty("umbracoFile").Value.ToString().Trim().Length > 0)
			                        {
			                            newDefaultImage = Server.UrlEncode(temp.getProperty("umbracoFile").Value.ToString());
			                        }
			                        else
			                        {
			                            Media tempDefault = new Media(DefaultImage);
			                            newDefaultImage = Server.UrlEncode(tempDefault.getProperty("umbracoFile").Value.ToString());
			                        }
                                }
                            }
                        }
                    }
                    else
                    {
                        Media temp = new Media(DefaultImage);
                        newDefaultImage = Server.UrlEncode(temp.getProperty("umbracoFile").Value.ToString());
                    }

                    Product realProd = ProductController.GetProduct(cross.ProductID);

                    dt.Rows.Add(cross.ProductID, realProd.UrlFriendly, cross.ProductName, newDefaultImage, cross.OurPrice);
                }
                StatusLabel.Text = dt.Rows.Count.ToString();

                if (dt.Rows.Count > 0)
                {
                    CrossSellProducts.DataSource = dt;
                    CrossSellProducts.DataBind();
                }
                else
                {
                    CrossSellProducts.Visible = false;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCrossSells();
            }
        }

    }
}